package exer;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @author tongchen
 * @create 2023-02-08 14:23
 */
public class ReentrantLockTest {
    public static void main(String[] args) {
        //创建对象
        ReentrantLock reentrantLock = new ReentrantLock(true);
        //需要手动进行上锁和解锁
        try {
            reentrantLock.lock();
            //创建condition对象
            Condition condition = reentrantLock.newCondition();
            condition.await();
            condition.signal();
            condition.signalAll();
            ReentrantReadWriteLock reentrantReadWriteLock = new ReentrantReadWriteLock();
            //获取读锁
            ReentrantReadWriteLock.ReadLock readLock = reentrantReadWriteLock.readLock();
            //获取写锁
            ReentrantReadWriteLock.WriteLock writeLock = reentrantReadWriteLock.writeLock();


        }catch (Exception e){
        e.printStackTrace();
        }finally {

            reentrantLock.unlock();
        }
        //
        ReentrantLock reentrantLock1 = new ReentrantLock();
        try {
            reentrantLock1.lock();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            reentrantLock1.unlock();
        }

    }
}
